package com.chenyi.data.structure.stack;

/**
 * @author by chenyi
 * @className ArrayStack
 * @date 2021/12/27 15:23
 */

public class ArrayStack {

    /**
     * 数组模拟栈
     */
    private int[] arr;

    /**
     * 最大容量
     */
    private int maxSize;

    /**
     * 栈顶
     */
    private int top = -1;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[this.maxSize];
    }

    /**
     * 入栈
     * @param e
     * @return
     */
    public boolean push(int e) {
        if (isFull()) {
            System.out.println("栈已满");
            return false;
        }
        arr[++top] = e;
        return true;
    }

    /**
     * 出栈
     * @return
     */
    public int pop() {
        if (isEmpty()) {
            System.out.println("栈为空");
            return -1;
        }
        return arr[top--];
    }

    /**
     * 查看栈中最后一个数据
     * @return
     */
    public int peekLast() {
        if (isEmpty()) {
            System.out.println("栈为空");
            return -1;
        }
        return arr[top];
    }

    /**
     * 查看栈是否已满
     * @return
     */
    public boolean isFull() {
        return top == maxSize - 1;
    }

    /**
     * 查看栈是否为空
     * @return
     */
    public boolean isEmpty() {
        return top == -1;
    }

}
